﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HostingDomainWebsite;
using Obout.Grid;

namespace Hosting
{
    public partial class Article : System.Web.UI.Page
    {

        private SiteMaster master;

        protected void Page_Load(object sender, EventArgs e)
        {
            if(Session["username"] == null)
            {
                Response.Redirect("AdminLogin.aspx");
                return;
            }
            
            master = Page.Master as SiteMaster;
            master.setCurrentNav("ArticleCategories", "Article");
            sArticleGroup.Attributes["onchange"] = "filterGrid(this)";
            FillGroupToComboBox();
        }

        #region [Fill Article Groups to combo box]
        //Load article group to Combo box
        protected void FillGroupToComboBox()
        {
            if (sAGroup != null)
            {
                sAGroup.Items.Clear();
            }
            Hosting.Class.ArticleGroups groups = new Hosting.Class.ArticleGroups();
            var g = groups.getAll("-1");
            foreach (var i in g)
            {
                ListItem list = new ListItem();
                list.Value = i.AGId.ToString();
                list.Text = i.AGName;
                sAGroup.Items.Add(list);
            }
        }
        #endregion

        #region [Grid inserts Article]
        protected void dgArticles_InsertCommand(object sender, GridRecordEventArgs e)
        {

            Hosting.Class.Articles articles = new Hosting.Class.Articles();
            int isValid = articles.AddValid(e.Record["AGId"].ToString(), e.Record["AName"].ToString());

            string active = "false";
            if (AActive.Checked)
            {
                active = "true";
            }

            if (isValid == 1)
            {
                int isInsert = articles.Add(Session["username"].ToString(), e.Record["AGId"].ToString(), e.Record["AName"].ToString(), e.Record["AContent"].ToString()
                    , Boolean.Parse(active));
                if (isInsert == 0)
                {
                    master.getMessageSuccess("Successfully!");
                }
                else
                {
                    master.getMessageError("There is an error!");
                }
            }
            else if (isValid == 2)
            {
                master.getMessageAttention("Article Group is null or empty");
            }
            else if (isValid == 3)
            {
                master.getMessageAttention("Article Group is not valid");
            }
            else if (isValid == 10)
            {
                master.getMessageAttention("Article title is null or empty");
            }
            else if (isValid == 11)
            {
                master.getMessageAttention("Article title is less than 6 characters");
            }
            else if (isValid == 12)
            {
                master.getMessageAttention("Article title is greater than 100");
            }
        }
        #endregion

        #region [Grid updates Article]
        protected void dgArticles_UpdateCommand(object sender, GridRecordEventArgs e)
        {

            Hosting.Class.Articles articles = new Hosting.Class.Articles();
            int isValid = articles.EditValid(e.Record["AId"].ToString(), e.Record["AGId"].ToString(), e.Record["AName"].ToString());

            string active = "false";
            if (AActive.Checked)
            {
                active = "true";
            }

            if (isValid == 1)
            {
                int isInsert = articles.Edit(e.Record["AId"].ToString(), e.Record["UserName"].ToString(), e.Record["AGId"].ToString()
                    , e.Record["AName"].ToString(), e.Record["AContent"].ToString(), Boolean.Parse(active));
            }
            else if (isValid == 2)
            {
                master.getMessageAttention("Article Group is null or empty");
            }
            else if (isValid == 3)
            {
                master.getMessageAttention("Article Group is not valid");
            }
            else if (isValid == 4)
            {
                master.getMessageAttention("Article Group is null or empty");
            }
            else if (isValid == 5)
            {
                master.getMessageAttention("Article Group is not valid");
            }
            else if (isValid == 10)
            {
                master.getMessageAttention("Article title is null or empty");
            }
            else if (isValid == 11)
            {
                master.getMessageAttention("Article title is less than 6 characters");
            }
            else if (isValid == 12)
            {
                master.getMessageAttention("Article title is greater than 100");
            }
        }
        #endregion

        #region [Grid deletes Article]
        protected void dgArticles_DeleteCommand(object sender, GridRecordEventArgs e)
        {
            Hosting.Class.Articles article = new Hosting.Class.Articles();
            var isDelete = article.delete(e.Record["AId"].ToString());
            if (isDelete != 0)
            {
                master.getMessageError("There is an error when delete an article!");
            }
        }
        #endregion
    }

}